package com.zys.el.user.util;

import com.zys.el.user.po.Field;
import com.zys.el.user.po.FieldMenu;
import com.zys.el.user.service.IFieldMenuService;
import com.zys.el.user.service.IFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class FieldUtil {
    @Autowired
    private IFieldService iFieldService;

    @Autowired
    private IFieldMenuService iFieldMenuService;

    Field field = new Field();

    //集合操作：对传进来的领域集合进行判断和操作
    public void FieldHandle(List<String> fields, Long PatentId) {
        for (String fieldName : fields) {
            //插入一个领域，先是否存在此领域，存在则不需要插入领域只需要插入此领域和拥有此领域的节点，不存在则插入领域和插入此领域和拥有此领域的节点
            //是否存在此领域
            Field qf = iFieldService.QueryFieldByName(fieldName);
//            System.out.println("field:" + qf);
            if (qf != null) {
                //存在就只需要操作中间表
//                System.out.println(qf + "++++++" + PatentId);
                addFieldMenu(qf, PatentId);
            } else {
                addField(fieldName);
                qf = iFieldService.QueryFieldByName(fieldName);
//                System.out.println(qf.getId());
                addFieldMenu(qf, PatentId);
            }

        }
    }

    //增加领域
    public void addField(String fieldName) {
        field.setId(null);
        field.setFieldName(fieldName);
        iFieldService.saveOrUpdate(field);
        System.out.println(field.getId());
    }

    //增加中间表
    public void addFieldMenu(Field qf, Long id) {
        try {
            FieldMenu fieldMenu = new FieldMenu();
            fieldMenu.setFieldId(qf.getId());
            fieldMenu.setParentId(id);
            iFieldMenuService.saveOrUpdate(fieldMenu);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


